<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="zlib.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.zlib.html">Zlib 函数</a></div>
 <div class="up"><a href="book.zlib.html">Zlib</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="zlib.examples" class="chapter">
 <h1>范例</h1>

 <p class="para">
  This example opens a temporary file and writes a test string 
  to it, then it prints out the content of this file twice.
 </p>
 <div class="example" id="example-784">
  <p><strong>Example #1 Small Zlib Example</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">tempnam</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'zlibtest'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'.gz'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;html&gt;\n&lt;head&gt;&lt;/head&gt;\n&lt;body&gt;\n&lt;pre&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Only&nbsp;a&nbsp;test,&nbsp;test,&nbsp;test,&nbsp;test,&nbsp;test,&nbsp;test,&nbsp;test,&nbsp;test!\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;open&nbsp;file&nbsp;for&nbsp;writing&nbsp;with&nbsp;maximum&nbsp;compression<br /></span><span style="color: #0000BB">$zp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gzopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w9"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;write&nbsp;string&nbsp;to&nbsp;file<br /></span><span style="color: #0000BB">gzwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$zp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;close&nbsp;file<br /></span><span style="color: #0000BB">gzclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$zp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;open&nbsp;file&nbsp;for&nbsp;reading<br /></span><span style="color: #0000BB">$zp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gzopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;read&nbsp;3&nbsp;char<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">gzread</span><span style="color: #007700">(</span><span style="color: #0000BB">$zp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;output&nbsp;until&nbsp;end&nbsp;of&nbsp;the&nbsp;file&nbsp;and&nbsp;close&nbsp;it.<br /></span><span style="color: #0000BB">gzpassthru</span><span style="color: #007700">(</span><span style="color: #0000BB">$zp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">gzclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$zp</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;open&nbsp;file&nbsp;and&nbsp;print&nbsp;content&nbsp;(the&nbsp;2nd&nbsp;time).<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">readgzfile</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)&nbsp;!=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Error&nbsp;with&nbsp;zlib&nbsp;functions!"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;/pre&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="zlib.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.zlib.html">Zlib 函数</a></div>
 <div class="up"><a href="book.zlib.html">Zlib</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
